import { store } from '@/stores/index'
import { useRouteHistoryStore } from '@/stores/modules/routeHistory'
import { handledRoutes, findRouteResult } from '@/router'
const routeHistoryStore = useRouteHistoryStore(store)

export const install = ({ router }) => {
  const routes = router.getRoutes()
  router.beforeEach(async (to, from) => {
    if (to.meta.currentActiveMenu) {
      if (routeHistoryStore.uniqueValidate(to.meta.currentActiveMenu)) {
        const result = findRouteResult(to.meta.currentActiveMenu, routes)
        if (result) {
          routeHistoryStore.historyPushback(result)
        }
      }
    }
    if (routeHistoryStore.uniqueValidate(to.path) && !to.meta.currentActiveMenu) {
      routeHistoryStore.historyPushback(to)
    }
    return true
  })

  router.afterEach(async (to, from) => {
    return true
  })
}
